<template>
    <el-container class="homeCon">
        <!-- 头部区域 -->
        <el-header class="homeHeader">
            <div>
                <img src="../assets/heima.png" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="checkout">退出</el-button>
        </el-header>
        <!-- 页面主体区域 -->
        <el-container>
            <!-- 左侧边框 -->
            <el-aside :width="isfold? '64px' : '200px'" class="homeAside">
                <div class="menufold" v-on:click="fold"> > </div>
                <!-- :unique-opened必须动态绑定不然true是个值不是布尔 -->
                <!-- router代表开启跳转连接模式 链接地址为index的值 -->
                <!-- unique-opened	是否只保持一个子菜单的展开 -->
                <!-- collapse	是否水平折叠收起菜单 -->
                <!-- collapse-transition	是否开启折叠动画 -->
                <!-- router	是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
                <!-- 变成路由模式后二级组件想要颜色必须default-active	当前激活菜单的 index  保存左侧菜单的激活状态-->
                <el-menu background-color="#333744" text-color="#fff" active-text-color="rgb(64, 158, 255)" :unique-opened="true" :collapse="isfold" :collapse-transition="false" :router="true" :default-active="active">
                    <!-- 点击一个所有都展开是因为 index控制 index值一样就都会展开   index只接受字符串 -->
                    <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
                        <template slot="title">
                            <!-- 动态绑定字体图标 -->
                            <i :class="iconobg[item.id]" class="iconobj">{{item.id}}</i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- router代表开启跳转连接模式 链接地址为index的值 -->
                        <el-menu-item :index="'/'+fronlet.path+''" v-for="fronlet in item.children" :key="fronlet.id" @click="activePath('/'+fronlet.path+'')">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{fronlet.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧主体区域 -->
            <el-main class="homeMain">
                <!-- 路由占位符 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>
<script>
    export default {
        data() {
            return {
                menulist:[],
                // 字体图标对象
                iconobg: {
                    '125':'iconfont icon-user',
                    '103':'iconfont icon-tijikongjian',
                    '101':'iconfont icon-shangpin',
                    '102':'iconfont icon-danju',
                    '145':'iconfont icon-baobiao'  
                },
                isfold:false,
                active:''
            }
        },
        created(){
            this.getlist()
            // 页面刷新后default-active的值
            this.active = window.sessionStorage.getItem('active') 
        },
        methods:{
            checkout() {
                window.sessionStorage.clear();
                this.$router.push('/login');
            },
            async getlist() {
                const {data: res} = await this.$http.get('menus')
                // console.log(res);
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.menulist = res.data
                // console.log(this.menulist);
            },
            fold() {
                this.isfold = !this.isfold
            },
            activePath(activep) {
                window.sessionStorage.setItem('active',activep);
                // 当前default-active的值
                this.active = activep;
            }
        }
    }
</script>
<style lang="less" scoped>
.homeHeader {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    color: #fff;
    padding-left: 0;
    align-items: center;
    > div{
        display: flex;
        align-items: center;
        span {
            margin-left: 15px;
        }
    }
}
.iconobj {
    margin-left: 15px;
}
.homeCon {
    height: 100%;
}
.homeAside {
    background-color: #333744;
    .el-menu {
        border-right:none;
    }
    .menufold {
        height: 30px;
        line-height: 30px;
        background-color: #4a5064;
        text-align: center;
        font-size: 24px;
        color:#fff;
    }
}
.homeMain {
    background-color: #eaedf1;;
}
</style>